package com.xiaoyu.lanling.feature.fate.viewholder

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import androidx.emoji.widget.EmojiTextView
import com.facebook.drawee.view.SimpleDraweeView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.coin.data.CoinData
import com.xiaoyu.lanling.feature.fate.model.MainFateNearbyUserItem
import com.xiaoyu.lanling.feature.fate.model.MainFateUserItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.UserAvatarDraweeView
import com.xiaoyu.lanling.view.text.UserNameTextView

class MainFateNearbyUserViewHolder : ViewHolderBase<MainFateNearbyUserItem>() {

    companion object {
        val sOnItemClickListener = View.OnClickListener { view ->
            val activity = App.getInstance().topActivity ?: return@OnClickListener
            val item = view.getViewTag<MainFateNearbyUserItem?>() ?: return@OnClickListener
            Router.instance.gotoUserActivity(activity, item.user)
        }

        val sOnAccostClickListener = View.OnClickListener { view ->
            val itemData = view.getViewTag<MainFateNearbyUserItem?>() ?: return@OnClickListener
            CoinData.accost(Any(), itemData.user.uid)
        }
    }

    private lateinit var mAvatar: UserAvatarDraweeView
    private lateinit var mName: UserNameTextView
    private lateinit var mVipIcon: SimpleDraweeView
    private lateinit var mTag: EmojiTextView
    private lateinit var mSoliloquy: EmojiTextView
    private lateinit var mAccost: ImageButton

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_main_fate_user, parent, false)
        mAvatar = view.findViewById(R.id.avatar)
        mName = view.findViewById(R.id.name)
        mVipIcon = view.findViewById(R.id.vip_icon)
        mTag = view.findViewById(R.id.tag)
        mSoliloquy = view.findViewById(R.id.soliloquy)
        mAccost = view.findViewById(R.id.accost)

        view.setOnClickDebounceListener(sOnItemClickListener)
        mAccost.setOnClickDebounceListener(sOnAccostClickListener)

        return view
    }

    override fun showData(position: Int, itemData: MainFateNearbyUserItem) {
        ImageViewUtil.loadImage(mAvatar, itemData.avatarLoadParams)
        mName.setUser(itemData.user)
        mName.setTextColor(AppContext.getColor(if (itemData.vipInfo.isVip) R.color.vip_name else R.color.colorOnSurface))
        mName.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, if (itemData.verify) R.drawable.icon_verify_verified else 0, 0)
        ImageViewUtil.loadImage(mVipIcon, itemData.vipInfo.imageLoadParam)
        mTag.text = itemData.tag
        mSoliloquy.text = itemData.soliloquy
        mCurrentView.setViewTag(itemData)
        mAccost.isSelected = itemData.selected
        mAccost.setViewTag(itemData)
    }
}